package com.mrper.jnindk;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.AppCompatSeekBar;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by admin on 2016/5/3.
 */
public class Player implements MediaPlayer.OnBufferingUpdateListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener, SurfaceHolder.Callback  {

    private int videoWidth,videoHeight;
    public MediaPlayer mediaPlayer;
    private SurfaceHolder surfaceHolder;
    private AppCompatSeekBar seekBar;
    private Timer timer = new Timer();

    public Player(SurfaceView surfaceView, AppCompatSeekBar seekBar){
        this.seekBar = seekBar;
        this.surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
//        surfaceHolder.setType();
        timer.schedule(timerTask,0,1000);
    }

    TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            if(mediaPlayer == null) return;
            if(mediaPlayer.isPlaying() && !seekBar.isPressed())
                handleProcess.sendEmptyMessage(0);
        }
    };

    Handler handleProcess = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int position = mediaPlayer.getCurrentPosition();
            int duration = mediaPlayer.getDuration();
            if(duration > 0){
                long pos = seekBar.getMax() * position / duration;
                seekBar.setProgress((int)pos);
            }
        }
    };

    public void play(){
        if(mediaPlayer!=null) {
            mediaPlayer.start();
        }else{

        }
    }

    public void playUrl(String videoUrl){
        try{
            mediaPlayer.reset();
            mediaPlayer.setDataSource(videoUrl);
            mediaPlayer.prepareAsync();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void pause(){
        if(mediaPlayer!=null) {
            mediaPlayer.pause();
        }else{

        }
    }

    public void stop(){
        if(mediaPlayer != null){
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try{
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setDisplay(surfaceHolder);
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaPlayer.setOnBufferingUpdateListener(this);
            mediaPlayer.setOnPreparedListener(this);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }

    @Override
    public void onBufferingUpdate(MediaPlayer mp, int percent) {
        seekBar.setSecondaryProgress(percent);
        int currentProgress = seekBar.getMax()*mediaPlayer.getCurrentPosition() / mediaPlayer.getDuration();
    }

    @Override
    public void onCompletion(MediaPlayer mp) {

    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        videoWidth = mediaPlayer.getVideoWidth();
        videoHeight = mediaPlayer.getVideoHeight();
        if(videoHeight != 0 && videoWidth != 0){
            mp.stop();
        }
    }

}
